﻿<%@ Control Language="VB" AutoEventWireup="false" CodeFile="Uc_stp_auth_department_fv.ascx.vb" Inherits="Apps_Shares_Uc_stp_auth_department_fv" %>
<%--==============================BEGIN Filter==============================--%>
<table id='tblList' width="80%" border="1" align="center" cellpadding="2" cellspacing="2" bordercolor="#999999">
    <thead id='t1' runat='server'>
        <tr align="center" bgcolor="#99FFFF">
            <td colspan="2" bgcolor="#FFFFFF" class="th_data">
                ค้นหาข้อมูล
            </td>
        </tr>
    </thead>
    <tbody id='t2' runat='server'>
        <tr align="left">
            <td width="25%" bordercolor="#CCCCCC" class="td_box">
                ระบบ :
            </td>
            <td width="75%" bordercolor="#CCCCCC" class="td_box">
                <asp:DropDownList ID='ddlSystem' runat='server' DataSourceID="ddlSystemSrc" DataTextField="main_system_name"
                    AppendDataBoundItems="true" DataValueField="main_system_code" AutoPostBack="true">
                    <asp:ListItem Text="ระบบทั้งหมด" Value="0"></asp:ListItem>
                </asp:DropDownList>
                <asp:SqlDataSource ID="ddlSystemSrc" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
                    SelectCommand="select main_system_name , main_system_code from stp_main_system where main_system_status = 1 and main_system_type > 0"
                    SelectCommandType='Text'></asp:SqlDataSource>
            </td>
        </tr>
    </tbody>
    <tbody id='t3' runat='server'>
        <tr align="center">
            <td colspan="2" bordercolor="#CCCCCC" class="td_data_end">
                <asp:Button ID='btnFilter' runat='server' Text='แสดงข้อมูล'  ValidationGroup='g1' />
                <asp:Button ID='btnMainMenu' Text='<%$ Resources:Resource,BTN_MAINMENU %>' runat='server' PostBackUrl="~/Apps/ViewSystem.aspx?Edit=2" />
            </td>
        </tr>
    </tbody>
    <tbody id='bDepartment' runat='server' visible='false'>
        <tr>
            <td width="25%" bordercolor="#CCCCCC" class="td_box" valign="top"><br />หน่วยงานที่ต้องการกำหนดสิทธิ์การใช้งาน :</td>
            <td width="75%" bordercolor="#CCCCCC" class="td_box"><br />
                <asp:ListBox ID="lbDepartment" runat="server" DataTextField="department_name" DataValueField="department_code"
                    Height="187px" DataSourceID='ddlStpDepartmentSrc' Width="100%" SelectionMode="Multiple">
                </asp:ListBox>
                <asp:ObjectDataSource ID="ddlStpDepartmentSrc" runat="server" SelectMethod="GetStpDepartmentByPrivilege"
                    TypeName="CodeMatrix.Business.GenericData">
                </asp:ObjectDataSource>
            </td>
        </tr>
    </tbody>
    <tbody id='ShowDepartment' runat='server'>
        <tr>
            <td colspan="2" align='center' class="text_head2">
                <asp:Literal ID='ltrDepartment' runat='server'></asp:Literal>
            </td>
        </tr>
    </tbody>
</table>
<%--==============================END Filter==============================--%>
<div style="margin:5px;display:block;width:100%">&nbsp;</div>
<%--==============================BEGIN Gridview==============================--%>
<asp:Literal ID="ltrMenuSystemTable" runat="server"></asp:Literal>

<script src="../Scripts/YAHOO.js" type="text/javascript"></script>

<script src="../Scripts/YUIJSON.js" type="text/javascript"></script>

<script src="../Scripts/utilities.js" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function() {
        isCheckAll(<%=rowTotal %>, <%=colTotal %>);
    });
    
    function saveAuthen() {
        var objMenu = $("input:checkbox");
        var objDepartment = [];
        $('#<%=lbDepartment.ClientID%> :selected').each(function(i, selected) {
            objDepartment[i] = $(selected).val();
        });

        if (objDepartment.length == 0) 
        {
            alert('กรุณาเลือกหน่วยงาน');
        }

        var cAuthDepartment = [];
        var i = 0; 
        for (i == 0; i < objDepartment.length; i++) {
            var j = 0;
            for (j == 0; j < objMenu.length; j++)
            {
                var re = new RegExp(/(col|row)/g);
                if (re.test(objMenu[j].id) == false)
                Array.add(cAuthDepartment, { auth_department_code: objMenu[j].authenDepCode, department_code: objDepartment[i], sub_menu_code: objMenu[j].subMenuCode, checked: objMenu[j].checked });
            }
        }

        if (cAuthDepartment.length > 0) {
            var jstring = YAHOO.lang.JSON.stringify(cAuthDepartment);
            $.ajax({
                url: "<%=WebServiceURL %>",
                cache: false,
                type: "POST",
                data: ({ jdata: jstring }),
                dataType: "json",
                async: false,
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    var result = eval('(' + XMLHttpRequest.responseXML.text + ')');
                    if (result.Message == 'success') {
                        alert('ข้อมูลถูกบันทึกแล้ว')
                        if (location.href.indexOf('Edit') > -1) {
                            window.location = 'stp_auth_department_list.aspx?Edit=true';
                        }
                        else {
                            window.location = 'stp_auth_department_fv.aspx?Insert=1';
                        }
                    } else {
                        alert(result.Message);
                    }
                },
                // Not used because result never return as success
                success: function(response) {
                    var result = eval('(' + response.d + ')');
                    if (result.Message == 'success') {
                        alert('ข้อมูลถูกบันทึกแล้ว')
                        if (location.href.indexOf('Edit') > -1) {
                            window.location = 'stp_auth_department_list.aspx?Edit=true';
                        }
                        else {
                            window.location = 'stp_auth_department_fv.aspx?Insert=1';
                        }
                    }
                }
            });
        }
        return false;
    }
</script>

